<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Participant.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Model_Participant extends Core_Model_Item_Abstract {
	protected $_owner_type = 'user';
	
	protected $_type = 'vote_participant';
	
	public function getShortType() {
		return 'participant';
	}
	
	public function getOwner() {
		return Engine_Api::_ ()->getItem ( 'user', $this->user_id );
	}
	
	public function isOwner(User_Model_User $user) {
		if (! $user->getIdentity ()) {
			return false;
		}
		
		return ( bool ) ($user->getIdentity () == $this->user_id);
	}
	
	public function getAuthorizationItem() {
		return Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $this->challenge_id )->current ();
	}
	
	public function getRichContent($view = false, $params = array()) {
		if ($this->ptype == 1) {
			$videoEmbedded = $this->compileYouTube ( $this->code, $view );
		}
		
		if ($this->ptype == 2) {
			$videoEmbedded = $this->compileVimeo ( $this->code, $view );
		}
		
		if ($this->ptype == 3 && $this->file_id) {
			$video_location = Engine_Api::_ ()->storage ()->get ( $this->file_id, $this->getType () )->map ();
			$videoEmbedded = $this->compileFlowPlayer ( $video_location, $view );
		}
		
		if ($view == false) {
			$video_duration = "";
			if ($this->duration) {
				if ($this->duration > 360)
					$duration = gmdate ( "H:i:s", $this->duration );
				else
					$duration = gmdate ( "i:s", $this->duration );
				
				if ($duration [0] == '0')
					$duration = substr ( $duration, 1 );
				
				$video_duration = "<span class='video_length'>" . $duration . "</span>";
			}
			
			$thumb = Zend_Registry::get ( 'Zend_View' )->itemPhoto ( $this, 'thumb.video.activity' );
			if ($this->photo_id) {
				$thumb = Zend_Registry::get ( 'Zend_View' )->itemPhoto ( $this, 'thumb.video.activity' );
			} else {
				$thumb = '<img alt="" src="application/modules/Vote/externals/images/nophoto/video.png">';
			}
			
			$thumb = '<a id="video_thumb_' . $this->participant_id . '" style="" href="javascript:void(0);" onclick="javascript:var myElement = $(this); myElement.style.display=\'none\';var next = myElement.getNext(); next.style.display=\'block\';">
                <div class="video_thumb_wrapper">' . $video_duration . $thumb . '</div>
                </a>';
			
			$title = "<a href='" . $this->getHref ( $params ) . "'>$this->title</a>";
			$tmpBody = strip_tags ( $this->description );
			$description = "<div class='video_desc'>" . (Engine_String::strlen ( $tmpBody ) > 255 ? Engine_String::substr ( $tmpBody, 0, 255 ) . '...' : $tmpBody) . "</div>";
			
			$videoEmbedded = $thumb . '<div id="video_object_' . $this->participant_id . '" style="display:none;">' . $videoEmbedded . '</div><div class="video_info">' . $title . $description . '</div>';
		}
		
		return $videoEmbedded;
	}
	
	public function compileYouTube($code, $view) {
		$embedded = '
    <object width="' . ($view ? "560" : "425") . '" height="' . ($view ? "340" : "344") . '">
    <param name="movie" value="http://www.youtube.com/v/' . $code . '&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1"/>
    <param name="allowFullScreen" value="true"/>
    <param name="allowScriptAccess" value="always"/>
    <embed src="http://www.youtube.com/v/' . $code . '&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1' . ($view ? "" : "&autoplay=1") . '" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="' . ($view ? "560" : "425") . '" height="' . ($view ? "340" : "344") . '" wmode="transparent"/>
    <param name="wmode" value="transparent" />
    </object>';
		
		return $embedded;
	}
	
	public function compileVimeo($code, $view) {
		$embedded = '
    <object width="' . ($view ? "640" : "400") . '" height="' . ($view ? "360" : "230") . '">
    <param name="allowfullscreen" value="true"/>
    <param name="allowscriptaccess" value="always"/>
    <param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' . $code . '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />
    <embed src="http://vimeo.com/moogaloop.swf?clip_id=' . $code . '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1' . ($view ? "" : "&amp;autoplay=1") . '" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' . ($view ? "640" : "400") . '" height="' . ($view ? "360" : "230") . '" wmode="transparent"/>
    <param name="wmode" value="transparent" />
    </object>';
		
		return $embedded;
	}
	
	public function compileFlowPlayer($location, $view) {
		$embedded = "
    <script type='text/javascript'>
    en4.core.runonce.add(function(){\$('video_thumb_" . $this->participant_id . "').removeEvents('click').addEvent('click', function(){flashembed('video_object_$this->participant_id',{src: '" . Zend_Registry::get ( 'Zend_View' )->baseUrl () . "/externals/flowplayer/flowplayer-3.1.5.swf', width: " . ($view ? "480" : "420") . ", height: " . ($view ? "386" : "326") . ", wmode: 'transparent'},{config: {clip: {url: '$location',autoPlay: " . ($view ? "false" : "true") . ", duration: '$this->duration', autoBuffering: true},plugins: {controls: {background: '#000000',bufferColor: '#333333',progressColor: '#444444',buttonColor: '#444444',buttonOverColor: '#666666'}},canvas: {backgroundColor:'#000000'}}});})});
    </script>";
		
		return $embedded;
	}
	
	public function getHref($params = array()) {
		$params = array_merge ( array ('route' => 'vote_general', 'reset' => true, 'controller' => $this->type, 'action' => 'view', 'pid' => $this->getIdentity () ), $params );
		
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function setPhoto($photo) {
		if ($photo instanceof Zend_Form_Element_File) {
			$file = $photo->getFileName ();
		} else if (is_array ( $photo ) && ! empty ( $photo ['tmp_name'] )) {
			$file = $photo ['tmp_name'];
		} else if (is_string ( $photo ) && file_exists ( $photo )) {
			$file = $photo;
		} else {
			throw new Exception ( 'invalid argument passed to setPhoto' );
		}
		
		$this->removePhotos ();
		
		$name = basename ( $file );
		$path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
		$params = array ('parent_id' => $this->getIdentity (), 'parent_type' => 'vote_participant' );
		
		// Save
		$storage = Engine_Api::_ ()->storage ();
		
		// Resize image (main)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 720, 720 )->write ( $path . '/m_' . $name )->destroy ();
		
		// Resize image (profile)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 200, 400 )->write ( $path . '/p_' . $name )->destroy ();
		
		// Resize image (normal)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 140, 160 )->write ( $path . '/in_' . $name )->destroy ();
		
		// Resize image (icon)
		$image = Engine_Image::factory ();
		$image->open ( $file );
		
		$size = min ( $image->height, $image->width );
		$x = ($image->width - $size) / 2;
		$y = ($image->height - $size) / 2;
		
		$image->resample ( $x, $y, $size, $size, 48, 48 )->write ( $path . '/is_' . $name )->destroy ();
		
		// Store
		$iMain = $storage->create ( $path . '/m_' . $name, $params );
		$iProfile = $storage->create ( $path . '/p_' . $name, $params );
		$iIconNormal = $storage->create ( $path . '/in_' . $name, $params );
		$iSquare = $storage->create ( $path . '/is_' . $name, $params );
		
		$iMain->bridge ( $iProfile, 'thumb.profile' );
		$iMain->bridge ( $iIconNormal, 'thumb.normal' );
		$iMain->bridge ( $iSquare, 'thumb.icon' );
		
		// Remove temp files
		@unlink ( $path . '/p_' . $name );
		@unlink ( $path . '/m_' . $name );
		@unlink ( $path . '/in_' . $name );
		@unlink ( $path . '/is_' . $name );
		
		$this->photo_id = $iMain->file_id;
		$this->save ();
		
		return $this;
	}
	
	public function removePhotos() {
		if (isset ( $this->photo_id ) && $this->photo_id != 0) {
			$storage = Engine_Api::_ ()->storage ();
			$file = $storage->get ( $this->photo_id );
			if ($file !== null)
				$file->remove ();
			
			$file = $storage->get ( $this->photo_id, 'thumb.profile' );
			if ($file !== null)
				$file->remove ();
			
			$file = $storage->get ( $this->photo_id, 'thumb.normal' );
			if ($file !== null)
				$file->remove ();
			
			$file = $storage->get ( $this->photo_id, 'thumb.icon' );
			if ($file !== null)
				$file->remove ();
		}
	}
	
	public function getVoting() {
		return Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $this->challenge_id )->current ();
	}
	
	public function getVoteCount() {
		return $this->vote_count;
	}
	
	protected function _delete() {
		$this->removePhotos ();
		
		if ($this->file_id) {
			$file = Engine_Api::_ ()->getItem ( 'storage_file', $this->file_id );
			if ($file) {
				$file->remove ();
			}
		}
		
		parent::_delete ();
	}
	
	public function comments() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'comments', 'core' ) );
	}
	
	public function likes() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'likes', 'core' ) );
	}

}